package com.wy2cloud.core.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * 系统名称 xx平台
 * 全局异常基础类
 * 创建时间 2017/9/13 18:10
 *
 * @author wuyy
 * @since 1.8
 */
public abstract class BaseGlobalExceptionAdvice {
    private static ConcurrentMap<String,Logger> loggers=new ConcurrentHashMap<>();

    protected abstract Logger getLocalLogger();
    public abstract String getError();

    protected Logger getClassLoggerByClassName(String className){
        if(!loggers.containsKey(className)){
            loggers.put(className, LoggerFactory.getLogger(className));
        }
        return loggers.get(className);
    }

    protected Logger getExceptionClassLogger(Throwable throwable){
        Logger log=null;
        if(throwable.getStackTrace().length>0){
            log=getClassLoggerByClassName(throwable.getStackTrace()[0].getClassName());
        }else{
            log=getLocalLogger();
        }
        return log;
    }
}